<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/users
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');
      	
     	$data = array('title' => 'FAT Application');
       	$this->load->view('default/header', $data);
		$this->load->view('default/users');
		$this->load->view('default/footer');
	}
	
	public function get_users()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');
      	
     	$usersObjectsArray = $this->user->get_all();
     	$usersJSON= json_encode($usersObjectsArray);
     	echo $usersJSON; 
	}
	
	public function save_user()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');
      	
     	$firstname = $this->input->post('firstname');
     	$lastname = $this->input->post('lastname');
     	$phone = $this->input->post('phone');
     	$email = $this->input->post('email');
     	
     	if($firstname && $lastname && $phone && $email){
           	$result = $this->user->insert($firstname, $lastname, $phone, $email);
           	if ($result){
              	echo json_encode(array('success'=>true));
              } else {
              	echo json_encode(array('msg'=>'Some errors occured.'));
              }
     	}else
           	echo json_encode(array('msg'=>'No needed data.'));
	}
	
	public function update_user($id)
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');
      	
     	$firstname = $this->input->post('firstname');
     	$lastname = $this->input->post('lastname');
     	$phone = $this->input->post('phone');
     	$email = $this->input->post('email');
     	
     	if($id && $firstname && $lastname && $phone && $email){
           	$result = $this->user->update($id, $firstname, $lastname, $phone, $email);
           	if ($result){
              	echo json_encode(array('success'=>true));
              } else {
              	echo json_encode(array('msg'=>'Some errors occured.'));
              }
     	}else
           	echo json_encode(array('msg'=>'No needed data.'));
	}
	
	public function remove_user()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');
      	
     	$id = $this->input->post('id');
     	if($id){
           	$result = $this->user->remove($id);
           	if ($result){
              	echo json_encode(array('success'=>true));
              } else {
              	echo json_encode(array('msg'=>'Some errors occured.'));
              }
     	}else
           	echo json_encode(array('msg'=>'No needed data.'));
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/users.php */